<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPermissionFn(1,'0')">添加权限</el-button>
        </div>

        <el-table border :data="permissionList" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.pid==='0'" type="text" @click="addPermissionFn(2,row.id)">添加</el-button>
              <el-button type="text" @click="editFn(row.id)">编辑</el-button>
              <el-button type="text" @click="delFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <perDialog ref="perDialog" :show-dialog.sync="showDialog" :is-edit="isEdit" @confirmEV="confirmFn" />
    </div>
  </div>
</template>

<script>
import perDialog from '@/views/permission/components/perDialog.vue'
import { getPermissionListAPI, addPermissionAPI, getPermissionDetailAPI, updatePermissionAPI, delPermissionAPI } from '@/api'
import { transTree } from '@/utils/index.js'
export default {
  components: {
    perDialog
  },
  data() {
    return {
      permissionList: [], // 权限管理列表数据
      showDialog: false, // 控制弹窗显隐
      type: 1, // 添加页面权限/按钮权限
      pid: '', // 新增权限点对象的pid值的使用
      isEdit: false // 同一个确定按钮的前提下,区分编辑还是添加的
    }
  },
  created() {
    // 调用获取权限管理列表的数据
    this.getPermissionList()
  },
  methods: {
    // 获取权限管理列表的数据
    async getPermissionList() {
      const res = await getPermissionListAPI().catch(err => err)
      if (!res.success) return this.$message.error(res.message)
      this.$refs.perDialog.perList = res.data
      // console.log(this.$refs.perDialog.perList)
      this.permissionList = transTree(res.data, '0')
    },
    // 添加的按钮点击事件
    addPermissionFn(type, id) {
      this.isEdit = false
      this.showDialog = true
      this.type = type
      this.pid = id
      this.$refs.perDialog.clickId = id
    },
    // 添加权限->接口调用
    async confirmFn(data) {
      if (this.isEdit) {
        const { message, success } = await updatePermissionAPI(data)
        if (!success) return this.$message.error(message)
        this.$message.success(message)
      } else {
        data.type = this.type
        data.pid = this.pid
        const { success, message } = await addPermissionAPI(data)
        if (!success) return this.$message.error(message)
        this.$message.success(message)
      }
      this.getPermissionList()
    },
    // 编辑按钮点击
    async editFn(id) {
      this.isEdit = true
      this.showDialog = true
      const { data } = await getPermissionDetailAPI(id)
      this.$refs.perDialog.formData = data
      this.$refs.perDialog.clickId = id
    },
    // 删除按钮
    async delFn(id) {
      try {
        await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', { type: 'warning' })
        const { message, success } = await delPermissionAPI(id)
        if (!success) return this.$message.error(message)
        this.getPermissionList()
        this.$message.success(message)
      } catch (e) {
        this.$message.info('取消删除')
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>

